package com.yunzhi.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.edais.util.DES3Util;
import com.edais.util.JsonUtil;
import com.yunzhi.domain.RequestHeader;
import com.yunzhi.domain.ResultCode;
import com.yunzhi.domain.ResultConstant;
import com.yunzhi.domain.ResultDTO;
import com.yunzhi.util.RequestUtil;
import com.yunzhi.util.ResponseUtil;
import com.yunzhi.util.TokenUtil;

@Controller
@RequestMapping("/yztest")
public class TestController {
	private static final Log logger = LogFactory.getLog(TestController.class);

	@RequestMapping("/test")
	public void test(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (!RequestUtil.validSign(request)) {
			logger.info("无效签名");
			ResponseUtil.write(response, ResultConstant.INVALID_SIGN);
			return;
		}
		if (!RequestUtil.validToken(request)) {
			logger.info("无效token");
			ResponseUtil.write(response, ResultConstant.INVALID_TOKEN);
			return;
		}
		RequestHeader header = RequestUtil.decodeHeader(request);
		Map map = RequestUtil.decodeData(request);

		JSONObject params = RequestUtil.decodeDataJson(request);
//		MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
//		logger.info("file：" + new String(file.getBytes(), "UTF-8"));
		
		ResultDTO<Map> resultDTO = new ResultDTO<>(ResultCode.SUCCESS);
		resultDTO.setRecord(map);
		String result  = resultDTO.toJsonString();
		ResponseUtil.write(response,  result);
	}

	@RequestMapping("/testNoToken")
	public void testNoToken(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (!RequestUtil.validSign(request)) {
			logger.info("无效签名");
			ResponseUtil.write(response, ResultConstant.INVALID_SIGN);
			return;
		}
		RequestHeader header = RequestUtil.decodeHeader(request);
		logger.info("header：" + header.toString());
		String data = request.getParameter("data");
		data = DES3Util.decode(request.getParameter("data"));
		ResultDTO<String> resultDTO = new ResultDTO<>(ResultCode.SUCCESS);
		resultDTO.setRecord(data);
		String result  = resultDTO.toJsonString();
		ResponseUtil.write(response,  result);
	}

	/**
	 * 生成token
	 * 
	 * @param response
	 * @param userId
	 */
	@RequestMapping("/genToken")
	public void genToken(HttpServletResponse response, @RequestParam("userId") String userId) {
		Map<String, Object> map = new HashMap<>();
		map.put("userId", userId);
		String token = TokenUtil.genToken(map);
		JSONObject json = new JSONObject();
		json.put("yzToken", token);
		ResponseUtil.writeNotEncode(response, json.toString());
	}

	/**
	 * 生成加密数据
	 * 
	 * @param response
	 * @param data
	 */
	@RequestMapping("/genData")
	public void genData(HttpServletResponse response, @RequestParam("data") String data) {
		ResponseUtil.write(response, data);
	}

	/**
	 * 解密数据
	 * 
	 * @param response
	 */
	@RequestMapping("/decodeData")
	public void decodeData(HttpServletRequest request, HttpServletResponse response) {
		try {
			ResponseUtil.writeNotEncode(response, JsonUtil.getJsonStringFromObject(RequestUtil.decodeData(request)));
		} catch (Exception e) {
			logger.error("解密失败", e);
		}
	}
}